1 Introduction

R is a programming language designed originally for performing statistical analyses. However, it’s potential has been increased and nowadays it can be used for general data analysis, creating atractive plots or even for creating websites! You can download R from the official repository: CRAN.

If you have used spreadsheets to calculate stuff, you have already programmed.

In R, you can assign values to variables using <-. You don’t have to type it everytime, just Alt+-!

variable <- 2342
variable
## [1] 2342

You can use R from the unix command line or download an Integrated Development Environment (IDE) such as RStudio.

2 Working with R in RStudio

  1. Code editor. In your code editor you can create, open and save your Rscripts. Write your code and run it whenever you want using Ctrl + Enter.
  2. R Console. This is where your R code is ran. You can run it from the code editor or directly typing things in the console.
  3. Environment and History. Here you can find the objects and variables that are loaded in R and the history of commands you have ran.
  4. Plots, help, files. All plots you generate and R documentation will appear in this pane.

3 R basics

3.1 Data types

The basic types of variables that can be represented in R are the following:

  • Numeric. 332, 0.234.
  • Character. "Barcelona", "woman". A special type of character is the class factor.
  • Logical. TRUE or FALSE.

You can check the type of variable using class().

Create one variable of each of the above-mentioned basic types and check that the type is correct using the class() function.
var <- "text" # character
class(var)
## [1] "character"
var <- 3242 # numeric
class(var)
## [1] "numeric"
var <- TRUE # logical
class(var)
## [1] "logical"

3.2 Arithmetic operators

Operator Description
+ addition
- subtraction
* multiplication
/ division
^ or ** exponentiation
3+4*5.2/2^2
## [1] 8.2

3.3 Logical operators

Operator Description
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
# Works with numerics
3 > 2
## [1] TRUE
# And also with characters
"class"=="class"
## [1] TRUE

3.4 Data structures

We can combine several data types into more complex structures. The different structures one can create in R are the following:

  • Vectors. Consists on a concatenation c() of values. The type of all values should be the same.
vector <- c(0,1,2,3,4,6)
vector
## [1] 0 1 2 3 4 6
  • Matrices. Are basically vectors, but with 2 dimensions (rows and columns). You can create a matrix using matrix(). You can check which arguments you can pass to the matrix function typing ?matrix or help(matrix)
mat <- matrix(vector, nrow=2)
mat
##      [,1] [,2] [,3]
## [1,]    0    2    4
## [2,]    1    3    6
As an example we just generated a vector of numbers. Try to generate a vector with letters (characters) from ‘a’ to ‘f’ and then convert it to a matrix with 2 columns.
vector <- c('a', 'b', 'c', 'd', 'e', 'f')
mat <- matrix(vector, ncol=2)
mat
##      [,1] [,2]
## [1,] "a"  "d" 
## [2,] "b"  "e" 
## [3,] "c"  "f"
  • Dataframes. Dataframes are very much like spreadsheets: each column is information on one variable and each row is an instance (for example, a patient). When constructing a dataframe, you can use different vectors to represent different information. The only requierement is that all vectors have to be the same length.
patients <- data.frame(patientID=1:4,
                       gender=c("male", "female", "male", "female"),
                       age=c(23, 45, 55, 22),
                       dead=c(F, T, T, F))
patients
##   patientID gender age  dead
## 1         1   male  23 FALSE
## 2         2 female  45  TRUE
## 3         3   male  55  TRUE
## 4         4 female  22 FALSE
patients$age # to select one column
## [1] 23 45 55 22
  • Lists. Lists are concatenations of any data type or data structure. The first element of your list can be a vector, the second one a matrix and the third one a data frame. You can number elements in your list and access each data structure using listName[[1]].
list <- list(name="donors",
             patientList=patients)
list
## $name
## [1] "donors"
## 
## $patientList
##   patientID gender age  dead
## 1         1   male  23 FALSE
## 2         2 female  45  TRUE
## 3         3   male  55  TRUE
## 4         4 female  22 FALSE
list[[1]]
## [1] "donors"
list[["name"]]
## [1] "donors"

3.5 Functions

There are many basic functions that are already loaded into R. For example, data.frame() is a function that generates data.frames and help() is a function that loads the manual for a specific R function.

You can create you own functions in R as follows:

sq <- function(x) {
  square <- x*x
  return(square)
}

sq(2)
## [1] 4

Functions are approppriate when you are copy&pasting operations many times, but changing tiny details. It might be more time consuming to create a function, but then you an run it with the parameters you want without any additional effort.

Many times, there are functions out there for the things you want to do. This functions are wrapped in packages and you can download them and use them freely!

4 Functions and packages

5 Loading data